package com.fairytask.engine.components.button
{
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.managers.ResManager;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class ButtonItem extends Sprite
	{
		private var _bgSpace		:Sprite;
		private var _iconSpace		:Sprite;
		private var _txtLabel		:Label;
		private var _normalMC		:MovieClip;
		private var _overMC		:MovieClip;
		private var _downMC		:MovieClip;
		private var _hotArea		:Sprite;
		
		//皮肤
		private var _skin			:String	=	"";
		private var _icon			:DisplayObject;
		
		//设定
		private var _itemWidth		:Number	=	0;
		private var _itemHeight	:Number	=	0;
		private var _currentSkin	:MovieClip;
		
		//数据
		private var _id			:int	=	0;
		private var _label			:String	=	"";
		private var _listData		:Object;
		
		public function ButtonItem(w:Number,h:Number,str:String,itemSkin:String="")
		{
			super();
			_itemWidth	=	w;
			_itemHeight	=	h;
			
			initGUI(str);
			initEvent();
			skin =itemSkin;
			buttonMode	=	true;
		}
		private function initGUI(str:String):void
		{
			_label				=	str;
			_txtLabel			=	new Label();
			_txtLabel.color		=	"FFFF00";
			_txtLabel.htmlStr	=	_label;
			_bgSpace			=	new Sprite();
			
			_hotArea			=	new Sprite();
			
			_iconSpace			=	new Sprite();
			
			_txtLabel.mouseEnabled	=	_iconSpace.mouseEnabled	=	false;
			
			addChild(_bgSpace);
			addChild(_txtLabel);
			addChild(_iconSpace);
			
			addChild(_hotArea);
			
			drawBG();
		   
			alignTxt();
			
			drawHotArea();

		

		}
		
		private function initEvent():void
		{
			addEventListener(MouseEvent.ROLL_OVER,showOver);
			addEventListener(MouseEvent.ROLL_OUT,showOut);
			addEventListener(MouseEvent.MOUSE_DOWN,showDown);
			addEventListener(MouseEvent.MOUSE_UP,showUP);
		}
		
		private function drawBG():void
		{
			_bgSpace.graphics.clear();
			_bgSpace.graphics.beginFill(0xff0000,0);
			if(!_itemHeight)
			{
				_itemHeight	=	25;
			}
			_bgSpace.graphics.drawRect(0,0,_itemWidth,_itemHeight);
			_bgSpace.graphics.endFill();
		}
		
		private function drawHotArea():void
		{
			_hotArea.graphics.clear();
			_hotArea.graphics.beginFill(0xff0000,0);
			_hotArea.graphics.drawRect(0,0,_itemWidth,_itemHeight);
			_hotArea.graphics.endFill();
		}
		
		private function alignTxt():void
		{
			_txtLabel.x	=	(width-_txtLabel.width)/2;
			_txtLabel.y	=	(height-_txtLabel.height)/2;
		}
		
		private function showOver(e:MouseEvent):void
		{
			if(_currentSkin!=_overMC)
			{
				clearBG();
				_bgSpace.addChild(_overMC);
				_currentSkin	=	_overMC;
			}
		}
		
		private function showOut(e:MouseEvent):void
		{
			if(_currentSkin!=_normalMC)
			{
				clearBG();
				_bgSpace.addChild(_normalMC);
				_currentSkin	=	_normalMC;
			}
		}
		
		private function showDown(e:MouseEvent):void
		{
			if(_currentSkin!=_downMC)
			{
				clearBG();
				_bgSpace.addChild(_downMC);
				_currentSkin	=	_downMC;
			}
		}
		
		private function showUP(e:MouseEvent):void
		{
			if(_currentSkin!=_overMC)
			{
				clearBG();
				_bgSpace.addChild(_overMC);
				_currentSkin	=	_overMC;
			}
		}
		
		private function clearBG():void
		{
			while(_bgSpace.numChildren>0)
			{
				_bgSpace.removeChildAt(0);
			}
		}
		
		override public function set width(val:Number):void
		{
			_itemWidth		=	val;
			drawBG();
			resizeSkin();
			alignTxt();
			drawHotArea();
		}
		override public function set height(val:Number):void
		{
			_itemHeight		=	val;
			drawBG();
			resizeSkin();
			alignTxt();
			drawHotArea();
		}
		
		private function resizeSkin():void
		{
			if(_skin!="")
			{
				_normalMC.width	=	_overMC.width	=	_downMC.width	=	_itemWidth;
				_normalMC.height=	_overMC.height	=	_downMC.height	=	_itemHeight;
			}
		}
		
		public function get skin():String
		{
			return _skin;
		}
		
		public function set skin(value:String):void
		{
			clearBG();
			_skin = value;
			if(_skin=="")
			{
				drawBG();
			}else{
				var res:MovieClip	=	ResManager.newResClass(_skin) as MovieClip;
				_normalMC			=	res.Button_normal;
				_overMC			=	res.Button_over;
				_downMC			=	res.Button_down;
				resizeSkin();
				_normalMC.x		=	_overMC.x	=	_downMC.x	=	0;
				_normalMC.y		=	_overMC.y	=	_downMC.y	=	0;
				_bgSpace.addChild(_normalMC);
				_currentSkin	=	_normalMC;
			}
		}
		
		public function set icon(value:DisplayObject):void
		{
			while(_iconSpace.numChildren>0)_iconSpace.removeChildAt(0);
			_icon	=	value;
			if(_icon!=null)
			{
				_iconSpace.addChild(_icon);
			}
		}
		
	
		public function get label():String
		{
			return _label;
		}
		
		public function set label(value:String):void
		{
			_label = value;
			_txtLabel.htmlStr	=	_label;
			alignTxt();
		}
		
		public function get listData():Object
		{
			return _listData;
		}
		
		public function set listData(value:Object):void
		{
			_listData = value;
		}
		
		
	}
}